<?
	/* Se asegura que el archivo es incluido por el index */
		defined( 'LOAD_MODULE_ACTION' ) or die( 'No esta permitido el acceso directo a ' . __FILE__ );
	/* Cuando se hace el include del archivo automaticamente crea un objeto y lo guarda en una variable temporal */
		$this->oTmpObject = new Organizacion_area_lst( $this );

class Organizacion_area_lst extends Lst
	{
	function Organizacion_area_lst( &$oModule )
		{
			$this->Lst( $oModule );
			$this->aFields['cKeyLookUp'] = getParam( PARAM_KEYLOOKUP );
		}
	function setLevel()
		{
			switch ( $this->getOptionView() )
			{
				case 'T':
					$this->aShowColumns = array( 'Organizacion', 'Area', 'Personal' );
					break;
				case 'L':
					$this->aShowColumns = array( 'Organizacion', 'Area', 'Depende' );
					break;
			}
			if ( array_key_exists( 'ncOrganizacion' , $this->aFields )  ) unset($this->aShowColumns[array_search( 'Organizacion',$this->aShowColumns)]);
		}
	function initialize()
		{
			$this->oFiltro = $this->oModule->loadClass( 'organizacion_area_frm' );
			$this->aFields = array_merge ( (array)$this->aFields,  (array)$this->oFiltro->getFieldArray() );
			
			$this->setTitle ( "Organizacion - Areas" );
			$this->setSummary( "Esta tabla contiene el listado de las areas de una Organizacion" );
			//$this->setExpandAll();
			$this->addColumn ( 'Organizacion', '', true );
			$this->addColumn ( 'Area', 'cdArea');
			$this->addColumn ( 'Depende', 'cdAreaPadre' );
			$this->addColumn ( 'Responsable', '' );
			$this->addColumn ( 'Telefono', 'cTelefono' );
			$this->addColumn ( 'Email', 'cEmail' );
			$this->addColumn ( 'Personal', '' );
			$this->setColumnAction ( 'Nombre', VER_PERSONA, 'ncPersona');
			$this->setColumnAction ( 'Area', CONSULTAR_PERSONAS_DE_UNA_ORGANIZACION, 'ncOrganizacion,ncArea', 'bRecursivo=1');
			$this->setColumnAction ( 'Depende', CONSULTAR_PERSONAS_DE_UNA_ORGANIZACION, 'ncOrganizacion,ncArea:ncAreaPadre' );

		}
	function makeColumn ( $aRow, $cField )
		{
			$cReturn = '';
			switch( $cField )
			{
				case 'Responsable':
					$cReturn .=  $aRow['cApellido'] . ', ' . $aRow['cNombre'];
					break;
				case 'Personal':
					$cReturn = "<hr/>";
					$cSql =  "SELECT Per.cApellido, Per.cNombre, OPe.ncPersona
									FROM atOrganizacionPersona as OPe
									JOIN atPersona as Per
										ON Per.ncPersona = OPe.ncPersona
									WHERE OPe.ncOrganizacion= " . $aRow['ncOrganizacion'] .  " AND ncArea=" . $aRow['ncArea']  ;
					$aMatrix = $this->oModule->oDatabase->getMatrix( $cSql );
					foreach ( $aMatrix as $aPersona )
					{
						$cReturn .= $this->oModule->makeAction( $aPersona['cApellido'] . ', ' . $aPersona['cNombre'], MODIFICAR_EL_CARGO_DE_UNA_PERSONA , "ncOrganizacion=" . $aRow['ncOrganizacion'] .  "&amp;ncPersona=" . $aPersona['ncPersona'], '', VENTANA_SECUNDARIA, 'body_main' ). '<br/>';
					}
					break;
				case 'KeyLookUp':
				case 'KeyTree':
					$cReturn = $aRow['ncArea'];
					break;
				case 'DescriptionLookUp':
					$cReturn =  $aRow['cdArea'] ;
					break;
				break;
				default:
					return false;
				break;
			}
			return $cReturn;
		}
	function query()
		{
			// Arma el Query
				$cSql = "SELECT Org.cdOrganizacion, OAr.ncArea, OAr.ncOrganizacion, OAr.cdArea, OAr.cTelefono, OAr.cEmail, OA2.cdArea as cdAreaPadre, OA2.ncArea as ncAreaPadre, OAr.ncResponsable as ncPersona, Per.cNombre, Per.cApellido
							FROM atOrganizacionArea as OAr
							JOIN atOrganizacion as Org
								ON Org.ncOrganizacion = OAr.ncOrganizacion
							LEFT JOIN atPersona as Per
								ON Per.ncPersona = OAr.ncResponsable
							LEFT JOIN atOrganizacionArea as OA2
								ON OA2.ncOrganizacion = OAr.ncOrganizacion
								AND OA2.ncArea = OAr.ncAreaPadre";
			// Arma el Filtro de la Consulta
				$this->oWhere->addArray ( $this->oFiltro->getFilterdArray() , 'OAr' );
				if ( $this->getTreeKey()  )
					$this->oWhere->addExpression ( 'OAr.ncAreaPadre', ' OAr.ncAreaPadre = ' . $this->getTreeKey() );
				elseif ( $this->isTree()  ) 
					$this->oWhere->addNull ( 'OAr.ncAreaPadre' );
				$this->oWhere->addKey ( $this->oDatabase->dbConcat( "' '", "OAr.cdArea" ), $this->aFields[cKeyLookUp] );
				return $cSql . $this->oWhere->make();
		}
	function makeListActions()
		{
			$this->oModule->setBack();
			$this->addListAction (  AGREGAR_AREA, "ncOrganizacion=" . $this->aFields['ncOrganizacion'], 'I', VENTANA_SECUNDARIA );
		}
	function makeRowActions( $aRow )
		{
			$cParametroRegistro =  "ncOrganizacion=" . $aRow["ncOrganizacion"] . "&amp;ncArea=" . $aRow["ncArea"];
			$this->addRowAction ( MODIFICAR_AREA, $cParametroRegistro , '', VENTANA_SECUNDARIA);
			$this->addRowAction ( ELIMINAR_AREA, $cParametroRegistro );
		}
	}
?>
